package com.yanze.schoolinformation.view.information.relax;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;

import com.example.xrecyclerview.XRecyclerView;
import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.adapter.rv.JokeAdapter;
import com.yanze.schoolinformation.base.BaseFragment;
import com.yanze.schoolinformation.databinding.FragmentJokeBinding;
import com.yanze.schoolinformation.model.bean.DuanZiBean;
import com.yanze.schoolinformation.model.bean.QsbkListBean;
import com.yanze.schoolinformation.utils.CommonUtils;
import com.yanze.schoolinformation.viewModule.relax.JokeNavigator;
import com.yanze.schoolinformation.viewModule.relax.JokeViewModel;

import java.util.List;
import java.util.Random;

/**
 * Created by MrZheng on 2018/4/28.
 */

public class JokeFragment extends BaseFragment<FragmentJokeBinding> implements JokeNavigator {

    private boolean mIsPrepared;
    private boolean mIsFirst = true;
    private JokeAdapter mAdapter;
    private JokeViewModel viewModel;

    @Override
    public int setContent() {
        return R.layout.fragment_joke;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = new JokeViewModel(this);
        viewModel.setNavigator(this);
        initRefreshView();

        // 准备就绪
        mIsPrepared = true;
        /**
         * 因为启动时先走loadData()再走onActivityCreated，
         * 所以此处要额外调用load(),不然最初不会加载内容
         */
        loadData();
    }

    private void initRefreshView() {
        bindingView.srlJoke.setColorSchemeColors(CommonUtils.getColor(R.color.colorTheme));
        bindingView.srlJoke.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                bindingView.srlJoke.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        viewModel.setPage(1);
                        viewModel.loadDuanziData();
                    }
                }, 500);
            }
        });
        bindingView.xrvJoke.setLayoutManager(new LinearLayoutManager(getActivity()));
        bindingView.xrvJoke.setPullRefreshEnabled(false);
        bindingView.xrvJoke.clearHeader();
        mAdapter = new JokeAdapter(getActivity());
        bindingView.xrvJoke.setAdapter(mAdapter);
        bindingView.xrvJoke.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
            }

            @Override
            public void onLoadMore() {
                int page = viewModel.getPage();
                viewModel.setPage(++page);
                viewModel.loadDuanziData();
            }
        });
    }

    @Override
    protected void loadData() {
        if (!mIsPrepared || !mIsVisible || !mIsFirst) {
            return;
        }

        bindingView.srlJoke.setRefreshing(true);
        bindingView.srlJoke.postDelayed(new Runnable() {
            @Override
            public void run() {
                viewModel.loadDuanziData();
            }
        }, 100);
    }

    @Override
    public void loadListFailure() {
        bindingView.xrvJoke.refreshComplete();
        if (bindingView.srlJoke.isRefreshing()) {
            bindingView.srlJoke.setRefreshing(false);
        }
        if (mAdapter.getItemCount() == 0) {
            showError();
        }
    }

    @Override
    public void showAdapterView(List<DuanZiBean> beans) {
        mAdapter.clear();
        mAdapter.addAll(beans);
        mAdapter.notifyDataSetChanged();

        bindingView.xrvJoke.refreshComplete();
    }

    @Override
    public void refreshAdapter(List<DuanZiBean> beans) {
        mAdapter.addAll(beans);
        mAdapter.notifyDataSetChanged();

        bindingView.xrvJoke.refreshComplete();
    }

    @Override
    public void showListNoMoreLoading() {
        bindingView.xrvJoke.noMoreLoading();
    }

    @Override
    public void showLoadSuccessView() {
        showContentView();
        bindingView.srlJoke.setRefreshing(false);
    }

    @Override
    protected void onRefresh() {
        viewModel.loadDuanziData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.onDestory();
    }
}
